package org.qydjk.console.controller.seller;

import org.qydjk.common.controller.BaseController;
import org.qydjk.common.pojo.SellerAppointment;
import org.qydjk.common.util.BeanUtil;
import org.qydjk.common.util.ListUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by ASUS on 2017/12/28.
 */
@RequestMapping("/sellerAppointment/")
@Controller
public class SellerAppointmentController extends BaseController{

    /**
     * 跳转到商家预约
     * @return
     */
    @RequestMapping("/sellerAppointment.do")
    public String sellerAppointment(){
        return "/seller/sellerAppointment";
    }

    /**
     * 日期预约数查询
     * @param sellerId
     * @param searchYear
     * @param searchMonth
     * @return
     */
    @RequestMapping("/appointmentInfo.do")
    @ResponseBody
    public Object appointmentInfo(String sellerId,String searchYear,String searchMonth) {
        HashMap map = new HashMap();
        HashMap dateMap = new HashMap();
        try {
            if (searchMonth.length()==1){
                searchMonth="0"+searchMonth;
            }
            String findInfoSql = "SELECT a.appointment_date,a.num,a.use_num,a.residual_num   " +
                    " FROM qy_seller_appointment a" +
                    " WHERE a.seller_id = "+sellerId+"" +
                    " AND a.appointment_date LIKE '"+searchYear+"-"+searchMonth+"%'";

            List<Map<String, Object>> list = baseService.findMapBySQL(findInfoSql);
            map.put("infoList", list);

            dateMap.put("year", searchYear);
            dateMap.put("month", searchMonth);
            map.put("date", dateMap);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 跳转到设置预约时间页
     * @param model
     * @param selectSellerId
     * @param selectDate
     * @return
     */
    @RequestMapping("/setAppointmentPage.do")
    public String  setAppointmentPage(Model model,String selectSellerId,String selectDate){
        try {
            model.addAttribute("selectSellerId",selectSellerId);
            model.addAttribute("selectDate",selectDate);
            String findsaSql = "SELECT a.id,a.num,a.use_num  " +
                " FROM qy_seller_appointment a " +
                " WHERE a.seller_id = "+selectSellerId+" " +
                " AND a.appointment_date LIKE '"+selectDate+"%'";

            List<Map<String, Object>> list = baseService.findMapBySQL(findsaSql);
            model.addAttribute("list",list);


        } catch (Exception e) {
            e.printStackTrace();
        }
        return  "seller/setAppointment";

    }

    /**
     * 设置预约时间
     * @param startDate
     * @param endDate
     * @param sellerAppointment
     * @return
     */
    @RequestMapping("/setAppointment.do")
    @ResponseBody
    public Object  setAppointment(String startDate, String endDate, SellerAppointment sellerAppointment){
        try{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date start = sdf.parse(startDate);
            Date end = sdf.parse(endDate);
            Calendar cal = Calendar.getInstance();
            cal.setTime(start);
            if (end.getTime()>=start.getTime()){
                while (end.getTime()>=start.getTime()){
                    SellerAppointment sa = new SellerAppointment();
                    BeanUtil.copyProperties(sellerAppointment,sa);
                    sa.setAppointmentDate(start);

                    Date date = new Date();

                    sa.setResidualNum(sa.getNum()-sa.getUseNum());
                    String startDateStr = sdf.format(start);
                    sa.setUpdateTime(date);

                    String findDateIdSql = "SELECT d.id  " +
                            "FROM qy_date d " +
                            "WHERE d.qy_date = '"+startDateStr+"'";
                    List<Map<String, Object>> dateList = baseService.findMapBySQL(findDateIdSql);
                    if (!ListUtil.isNullOrEmpty(dateList)){
                        sa.setDateId(Integer.valueOf(String.valueOf(dateList.get(0).get("id"))));
                    }

                    //是否已设置
                    String findsaSql = "SELECT a.id  " +
                            " FROM qy_seller_appointment a " +
                            " WHERE a.seller_id = "+sa.getSellerId()+" " +
                            " AND a.appointment_date LIKE '"+startDateStr+"%'";
                    List<Map<String, Object>> list = baseService.findMapBySQL(findsaSql);
                    if (ListUtil.isNullOrEmpty(list)){
                        //保存
                        sa.setCreateTime(date);
                        baseService.save(sa);
                    }else{
                        //更新
                        sa.setId(Integer.valueOf(String.valueOf(list.get(0).get("id"))));
                        baseService.update(sa);
                    }


                    cal.add(Calendar.DAY_OF_MONTH,1);
                    start = cal.getTime();
                }


            }else{
                return renderError("结束时间不可小于开始时间!");
            }
            return  renderSuccess("设置成功");
        } catch (Exception e) {
            e.printStackTrace();
            return  renderError("设置失败!");
        }
    }
}
